#include "Array.hpp"

void Array::print(std::ostream& out)
{
    out << YELLOW <<"\n" ;
    for ( size_t l=0; l<width_; ++l)
        out << "======  " ;
    out << std::endl;
    for ( size_t j=0; j<width_; ++j)
    {
        for ( size_t i=0; i<width_; ++i)
            out << std::fixed <<std::setprecision(5) <<array_dyn[(i*width_+j)] << " ";
        out << "\n";
    }
    out << WHITE <<"\n" ;
}

void Array::print2(std::ostream& out)
{
    out <<"#[x,y]\tvalue\n";
    int sizeArray=width_*width_;
    int auxX=0;
        for(int i=0;i<sizeArray;++i)
        {
            out <<"["<<auxX<<","<<i-auxX*width_<<"]\t"<<array_dyn[i]<<"\n";
            if((i+1)%width_==0)
            {
                ++auxX;
                out<<"\n";
            }
        }
}

void Array::resize(size_t level)
{
    delete [] array_dyn;
    width_= pow(2,level)+1;
    width_ = pow(2,level)+1;
    array_dyn = new real [width_*width_] ();
}

real Array::calcL2Norm() const
{
    real norm=0.0;
    for ( size_t q = 1; q <=width_; ++q)
    {
        for ( size_t p = 1; p <=width_; ++p)				
            norm +=(array_dyn[q*width_+p])*(array_dyn[q*width_+p])  ;
    }

    norm= sqrt(1.0/(width_*width_)*norm);
    return norm;
}

real Array::infNorm() const
{
    size_t sizeArray=width_*width_;
    real max=array_dyn[0];
    for(size_t i=0;i<sizeArray;++i)
    {
        if(max<array_dyn[i])
            max=array_dyn[i];
    }
    return max;
}

void Array::fill(const real& in)
{ 
    for ( size_t n=0; n<width_*width_ ; ++n)
        array_dyn[n]=in;
}

void Array::setBoundary(real value)
{
    for ( size_t q = 1; q <=width_; ++q)
    {
        array_dyn[q*width_+0] = value;
        array_dyn[q*width_+width_] = value;
    }
    for ( size_t p = 1; p <=width_; ++p)	
    {				
        array_dyn[0*width_+p] = value;
        array_dyn[width_*width_+p] = value;
    }
}